<?php

namespace app\user\controller;
use app\common\controller\UserBase;
use app\common\model\Customer as Kehu;
use app\common\model\CustomerTrack;
use app\common\model\CustomerRemind;
use app\common\model\UserConfig;

class Analysis extends UserBase
{
    protected function _initialize()
    {
        parent::_initialize();
        !config('website_status') && die(config('colse_explain'));
        $config = cache('db_config_data');
        if (!$config) {
            $config = [];
            foreach (model('config')->select() as $v) {
                $config[$v['group']][$v['name']] = $v['value'];
            }
            cache('db_config_data', $config);
        }
        config($config);
    }
    
    //参数设置
    public function index()
    {
        $type = input('type');
        switch ($type){
            case "1":
                $model = model('customer');
                $name = "新增客户数";
                break;
            case 2:
                $model = model('customer_track');
                $name = "新增跟踪数";
                break;
            case 3:
                $model = model('customer_remind');
                $name = "新增提醒数";
                break;
            case 4:
                $model = model('customer_order');
                $name = "新增交易数";
                break;
            default:
                $model = model('customer');
                $name = "新增客户数";
                break;
        }
        $dateframe = input('dateframe');
        if($dateframe){
            $start_time = strtotime(substr($dateframe,0,10));
            $end_time = strtotime(substr($dateframe,-10));
            $now_j = (($end_time-$start_time)/86400)+1;
            $array = array();
            $value = array();
            for($i=0;$i<$now_j;$i++){
                 $array[] = $dtrq = date('d',$start_time+$i*86400); //每隔一天赋值给数组
                 $day_begin = $start_time+$i*86400;
                 $day_end = ($start_time+$i*86400)+86399; 
                 $value[] = $model->where("create_time >='".$day_begin."' and create_time <= '".$day_end."'")->count();
            }
        }else{
            $start_time = mktime(0,0,0,date('m'),1,date('Y'));
            $end_time = mktime(23,59,59,date('m'),date('t'),date('Y'));
            $now_j = (($end_time-$start_time)/86400)+1;
            $array = array();
            $value = array();
            for($i=0;$i<$now_j;$i++){
                 $array[] = $dtrq = date('d',$start_time+$i*86400); //每隔一天赋值给数组
                 $day_begin = $start_time + $i*86400;
                 $day_end = ($start_time + $i*86400)+86399; 
                 $value[] = $model->where("create_time >='".$day_begin."' and create_time <= '".$day_end."'")->count();
            }
            $dateframe = date('Y-m-d',$start_time) .' - '. date('Y-m-d',$end_time);
        }
        //今日时间戳
        $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
        $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        
        $data['all_customer'] = Kehu::count();
        $data['today_customer'] = Kehu::where("create_time >=".$beginToday." and create_time <=".$endToday."")->count();
        $data['all_track'] = CustomerTrack::count();
        $data['today_track'] = CustomerTrack::where("create_time >=".$beginToday." and create_time <=".$endToday."")->count();
        $data['remind_no'] = CustomerRemind::where(['status'=>'0'])->count();
        $data['remind_yes'] = CustomerRemind::where(['status'=>'1'])->count();
        
        return $this->fetch(is_Client('index'),[
            'data' => $data,
            'array' => $array,
            'value' => $value,
            'name' => $name,
            'dateframe' => $dateframe
        ]);
    }
    
    
}
